import { 
  SHOWDRAGDROP,
  HIDDENDRAGDROP, 
  ADDDRAGDROP, 
  DESTROYDRAGDROP 
} from '_redux/actions/dragDrops'

const dragDropState = {
  dragDrop: [
  // {
  //   title: '',
  //   children: null,
  //   show: false,
  //   id: ''
  // }
  ],
  length: 0,
}
function dragDropReducer(state = dragDropState, action) {
  switch (action.type) {
  case SHOWDRAGDROP:
    return {
      ...state,
      dragDrop: dragDrop.map(dd => ({
        ...dd,
        show: dd.id === action.id ? true : dd.show
      }))
    }
  case HIDDENDRAGDROP:
    return {
      ...state,
      dragDrop: dragDrop.map(dd => ({
        ...dd,
        show: dd.id === action.id ? false : dd.show
      }))
    }
  case ADDDRAGDROP:
    const { children: { id } } = action
    for (let dd of state.dragDrop) {
      if (dd.id === id) {
        return state
      }
    }
    return {
      ...state,
      dragDrop: [
        ...state.dragDrop,
        {
          show: true,
          ...action.children
        }
      ],
      length: ++state.length
    }
  case DESTROYDRAGDROP:
    return {
      ...state,
      dragDrop: state.dragDrop.filter(dd => dd.id !== action.id)
    }
  default:
    return state
  }
}
export default dragDropReducer